﻿using System;
using System.Diagnostics;

namespace Microsoft.WowAddonStudio.Lua.Parser
{
    /// <summary>
    /// This is the default class that carries location
    /// information from the scanner to the parser.
    /// If you don't declare "%YYLTYPE Foo" the parser
    /// will expect to deal with this type.
    /// </summary>
    [DebuggerDisplay("{Line}:{Column} - {EndLine}:{EndColumn}")]
    public class LexLocation : IMerge<LexLocation>, IEquatable<LexLocation>
    {
        private int column;
        private int line;
        private int endColumn;
        private int endLine;

        /// <summary>
        /// Initializes a new instance of the <see cref="LexLocation"/> class.
        /// </summary>
        public LexLocation()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LexLocation"/> class.
        /// </summary>
        /// <param name="startLine">The sl.</param>
        /// <param name="startColumn">The sc.</param>
        /// <param name="endLine">The el.</param>
        /// <param name="endColumn">The ec.</param>
        public LexLocation(int startLine, int startColumn, int endLine, int endColumn)
        {
            this.line = startLine;
            this.column = startColumn;
            this.endLine = endLine;
            this.endColumn = endColumn;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LexLocation"/> class.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <param name="column">The column.</param>
        public LexLocation(int line, int column)
            : this(line, column, line, column)
        {
        }

        /// <summary>
        /// Gets or sets the column where the location starts.
        /// </summary>
        public int Column
        {
            get { return column; }
            set { column = value; }
        }

        /// <summary>
        /// Gets or sets the line where the location starts.
        /// </summary>
        public int Line
        {
            get { return line; }
            set { line = value; }
        }

        /// <summary>
        /// Gets or sets the column where the location ends.
        /// </summary>
        public int EndColumn
        {
            get { return endColumn; }
            set { endColumn = value; }
        }

        /// <summary>
        /// Gets or sets the line where the location ends.
        /// </summary>
        public int EndLine
        {
            get { return endLine; }
            set { endLine = value; }
        }

        /// <summary>
        /// Merges the location with another location.
        /// </summary>
        /// <param name="other">The other location.</param>
        /// <returns></returns>
        public LexLocation Merge(LexLocation other)
        {
            if (other == null)
                return this;

            return new LexLocation(Line, Column, other.EndLine, other.EndColumn);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                int result = column;
                result = (result * 397) ^ line;
                result = (result * 397) ^ endColumn;
                result = (result * 397) ^ endLine;
                return result;
            }
        }
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != typeof(LexLocation))
                return false;
            return Equals((LexLocation)obj);
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(LexLocation other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;
            return other.column == column && other.line == line && other.endColumn == endColumn && other.endLine == endLine;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return String.Format("{0}:{1} - {2}:{3}", line, column, endLine, endColumn);
        }

        /// <summary>
        /// Merges the location with another location.
        /// </summary>
        /// <param name="start">The start location.</param>
        /// <param name="end">The end location.</param>
        /// <returns></returns>
        public static LexLocation Merge(LexLocation start, LexLocation end)
        {
            if (start == null && end == null)
                return null;
            if (start == null)
                return end;
            if (end == null)
                return start;

            return new LexLocation(start.Line, start.Column, end.EndLine, end.EndColumn);
        }
    }
}